/*
 * OverviewMap.java
 *
 * Created on August 14, 2007, 8:26 PM
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.rtemaster.gui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.util.Iterator;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;
import org.fpdev.core.basenet.BLink;
import org.fpdev.apps.rtemaster.RouteMaster;
import org.fpdev.util.gui.MapCoordinates;

/**
 *
 * @author demory
 */
public class OverviewMap extends JPanel {

  private RouteMaster av_;
  private int width_,  height_;
  private double wx1_,  wy1_,  wx2_,  wy2_; // world bounds
  private double vx1_,  vy1_,  vx2_,  vy2_; // visible bounds 
  private int mx_,  my_,  dx_,  dy_;
  private BufferedImage bgImage_;
  private boolean updateBG_;

  /** Creates a new instance of OverviewMap */
  public OverviewMap(RouteMaster av, int w, int h, double wx1, double wy1, double wx2, double wy2) {
    av_ = av;
    wx1_ = wx1;
    wy1_ = wy1;
    wx2_ = wx2;
    wy2_ = wy2;
    double hd = ((double) w) * (wy2_ - wy1_) / (wx2_ - wx1_);
    width_ = w;
    height_ = (int) hd;
    dx_ = dy_ = 0;

    setPreferredSize(new Dimension(width_, height_));
    setBackground(Color.white);

    addMouseListener(new MouseAdapter() {

      @Override
      public void mouseClicked(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON1) {
          mClicked(e.getX(), e.getY());
        }
      }

      @Override
      public void mousePressed(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON1) {
          mx_ = e.getX();
          my_ = e.getY();
          dx_ = dy_ = 0;
        }
      }

      @Override
      public void mouseReleased(MouseEvent e) {
        updateMainMap();
      }
    });

    addMouseMotionListener(new MouseMotionAdapter() {

      @Override
      public void mouseDragged(MouseEvent e) {
        int dx = e.getX() - mx_;
        int dy = e.getY() - my_;
        dx_ += dx;
        dy_ += dy;
        mx_ = e.getX();
        my_ = e.getY();
        repaint();
      }
    });

    setBorder(new EtchedBorder(EtchedBorder.LOWERED));// BevelBorder(BevelBorder.LOWERED));
  }

  private void updateBG(Graphics2D g2d) {
    //System.out.println("updating ovmap bg");
    MapCoordinates cc = new MapCoordinates(wx1_, wy1_, wx2_, wy2_, width_, height_);
    RenderingHints renderHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    renderHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHints(renderHints);
    g2d.setColor(Color.LIGHT_GRAY);
    g2d.fillRect(0, 0, width_, height_);
    g2d.setColor(Color.MAGENTA);
    Iterator<BLink> mlinks = av_.getEngine().getDB().getMajorLinks().iterator();
    while (mlinks.hasNext()) {
      BLink link = mlinks.next();
      g2d.drawLine(cc.xToScreen(link.getX1()),
              cc.yToScreen(link.getY1()),
              cc.xToScreen(link.getX2()),
              cc.yToScreen(link.getY2()));
    }
    g2d.setColor(new Color(0, 0, 127));
    g2d.setStroke(new BasicStroke(2));
    Iterator<BLink> tlinks = av_.getEngine().getDB().getTransitLinks().iterator();
    while (tlinks.hasNext()) {
      BLink link = tlinks.next();
      g2d.drawLine(cc.xToScreen(link.getX1()),
              cc.yToScreen(link.getY1()),
              cc.xToScreen(link.getX2()),
              cc.yToScreen(link.getY2()));
    }
  }

  private void mClicked(int mx, int my) {
    //System.out.println("m "+mx+" "+my);


    double x1 = width_ * (vx1_ - wx1_) / (wx2_ - wx1_);
    double y1 = height_ * (vy1_ - wy1_) / (wy2_ - wy1_);
    double w = width_ * (vx2_ - vx1_) / (wx2_ - wx1_);
    double h = height_ * (vy2_ - vy1_) / (wy2_ - wy1_);
    y1 = height_ - y1 - h;
    //System.out.println("c "+((int) x1)+","+((int) y1)+" to "+((int) (x1+w))+","+((int) (y1+h)));

    if (mx >= x1 && my >= y1 && mx <= (x1 + w) && my <= (y1 + h)) {
      return;
    }
    int dx = mx - ((int) (x1 + w / 2));
    int dy = my - ((int) (y1 + h / 2));
    //System.out.println("d "+dx+" "+dy);
    double rdx = (double) dx / width_ * (wx2_ - wx1_);
    double rdy = (double) dy / height_ * (wy2_ - wy1_);
    //System.out.println("rd "+rdx+" "+rdy);
    av_.getGUI().getMapPanel().recenterDelta(rdx, -rdy);
  }

  private void updateMainMap() {
    double rdx = (double) dx_ / width_ * (wx2_ - wx1_);
    double rdy = (double) dy_ / height_ * (wy2_ - wy1_);
    dx_ = dy_ = 0;
    av_.getGUI().getMapPanel().recenterDelta(rdx, -rdy);
  }

  @Override
  protected void paintComponent(Graphics g) {
    try {
      super.paintComponent(g);
      Graphics2D g2d = (Graphics2D) g;

      if (bgImage_ == null) {
        updateBG_ = true;
      }
      if (updateBG_) {
        bgImage_ = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D bg = (Graphics2D) bgImage_.getGraphics();
        updateBG(bg);
        updateBG_ = false;
      }
      g2d.drawImage(bgImage_, 0, 0, this);

      double x1 = width_ * (vx1_ - wx1_) / (wx2_ - wx1_);
      double y1 = height_ * (vy1_ - wy1_) / (wy2_ - wy1_);
      double w = width_ * (vx2_ - vx1_) / (wx2_ - wx1_);
      double h = height_ * (vy2_ - vy1_) / (wy2_ - wy1_);
      y1 = height_ - y1 - h;
      g2d.setColor(Color.white);
      g2d.setStroke(new BasicStroke(4));
      g2d.drawRect((int) x1 + dx_, (int) y1 + dy_, (int) w, (int) h);
      g2d.setColor(Color.red);
      g2d.setStroke(new BasicStroke(2));
      g2d.drawRect((int) x1 + dx_, (int) y1 + dy_, (int) w, (int) h);
    } catch (Exception e) {
      //System.out.println("error drawing overview map");
      //e.printStackTrace();
    }
  }

  public void updateVisible(double vx1, double vy1, double vx2, double vy2) {
    vx1_ = vx1;
    vy1_ = vy1;
    vx2_ = vx2;
    vy2_ = vy2;
    repaint();
  }
}
